package com.tanhua.server.controller;

import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/users/phone")
public class ResetPhoneController {
    @Autowired
    private UserService userService;
    /**
     * 修改手机号，发送验证码
     */
    @PostMapping("sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        Long userId = UserHolder.getUserId();
        userService.sendVerificationCode(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号，校验验证码
     */
    @PostMapping("/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        String verificationCode = (String) map.get("verificationCode");
        boolean verification = userService.checkVerificationCode(verificationCode);
        return ResponseEntity.ok(verification);
    }
    /**
     * 修改手机号
     */
    @PostMapping
    public ResponseEntity resetPhone(@RequestBody Map map){
        String phone = (String) map.get("phone");
        userService.resetPhone(phone);
        return ResponseEntity.ok(null);
    }
}
